package com.cuitrip.business.bill;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.business.bill.model.BankInfo;
import com.cuitrip.business.bill.model.BankInfoList;
import com.cuitrip.business.tripservice.model.SelectMenu;
import com.cuitrip.business.tripservice.ui.SelectMenuFragment;
import com.cuitrip.business.webview.H5UrlMaker;
import com.cuitrip.business.webview.proxy.WalletHelpDocProxy;
import com.cuitrip.business.webview.proxy.WebViewProxy;
import com.cuitrip.component.item.ItemLayout;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.rx.bus.RxBus;
import com.lab.utils.ToastUtil;
import com.lab.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sheena on 16/4/27.
 */
public class AddBankAccountFragment extends CustomUiFragment implements IProxyCallback {

    @Bind(R.id.bank_name)
    ItemLayout bankNameLayout;

    @Bind(R.id.account_no)
    EditText etAccountNo;

    @Bind(R.id.beneficiary_name)
    EditText etBenefName;

    private ApiProxy apiProxy = new ApiProxy(this);
    private List<BankInfo> bankInfoList;
    private String type = "1";//1、银行卡 2、paypal
    private int bankId=-1;

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config = new CustomUiConfig();
        config.mLeftStyle = CustomUiConfig.AreaStyle.ICON_STYLE;
        config.mTopbarLeftAreaName = getString(R.string.back_icon);
        config.mTopbarMiddleAreaName = getString(R.string.wallet_account_operate_add);
        config.mRightStyle= CustomUiConfig.AreaStyle.ICON_STYLE;
        config.mTopbarRightAreaName=getString(R.string.query_icon);
        return config;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_add_bank_account);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this, getView());
    }

    @Override
    public void initData() {
        showNoCancelDialog();
        OrderBusiness.getBankList(apiProxy);

    }

    @OnClick(R.id.bank_name)
    public void selectBankName() {
        if (bankInfoList != null) {
            SelectMenuFragment selectMenuFragment = new SelectMenuFragment();
            final List<SelectMenu> selectMenuList = new ArrayList<>();
            for (int i = 0; i < bankInfoList.size(); i++) {
                SelectMenu selectMenu = new SelectMenu();
                selectMenu.key = String.valueOf(i);
                selectMenu.value = bankInfoList.get(i).getName();
                selectMenuList.add(selectMenu);
            }
            selectMenuFragment.setTitle(getString(R.string.wallet_account_bank));
            selectMenuFragment.setSelectMenuList(selectMenuList);
            showDialogFragment(selectMenuFragment);
            selectMenuFragment.setResultListener(new SelectMenuFragment.OnDataSelectBackListener() {

                @Override
                public void onSelect(SelectMenu selectMenu) {
                    int i = Integer.parseInt(selectMenu.key);
                    bankNameLayout.setmRightText(selectMenuList.get(i).value);
                    bankId = bankInfoList.get(i).getBankId();
                }
            });
        }
    }

    @OnClick(R.id.submit)
    public void submit() {
        String accountNo;
        String benefName;
        if (bankId==-1){
            ToastUtil.toast(getHostActivity(),getString(R.string.wallet_account_select_account_alert),0);
            return;
        }
        if (TextUtils.isEmpty(etAccountNo.getText()) || etAccountNo.getText().length()<12) {
            Utils.shakeView(etAccountNo);
            return;
        } else {
            accountNo = etAccountNo.getText().toString();
        }
        if (TextUtils.isEmpty(etBenefName.getText())) {
            Utils.shakeView(etBenefName);
            return;
        } else {
            benefName = etBenefName.getText().toString();
        }
        OrderBusiness.addUserAccount(apiProxy, type, accountNo, benefName, String.valueOf(bankId));

    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        if (!isAdded()){
            return false;
        }
        CtApiResponse response = (CtApiResponse) result.getData();
        if (response.isResponseNormal()) {
            if (response.getApiName().equals(ApiManager.GET_BANK_LIST)) {
                if (response.result instanceof BankInfoList) {
                    BankInfoList banks = (BankInfoList) response.result;
                    bankInfoList = banks.getList();
                    hideNoCancelDialog();
                }
            } else if (response.getApiName().equals(ApiManager.ADD_USER_ACCOUNT)) {
                RxBus.getDefault().send(new AccountAddEvent());
                onBackPressed();
            }
        } else {
            hideNoCancelDialog();
            ToastUtil.toast(getContext(), response.msg, 0);
        }
        return false;
    }

    @Override
    public void onTopbarLeftPress() {
        onBackPressed();
    }



    @Override
    public void onTopbarRightPress() {
        WebViewProxy.getInstance().browseWeb(getHostActivity(), WalletHelpDocProxy.formatUrl(H5UrlMaker.getWalletCreateAccount()),false);

    }
}
